package com.qf.security;

import com.alibaba.fastjson2.JSON;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2025/5/8
 * @desc 无认证时的异常处理
 */
@Component
public class AuthenticationEntryPointImpl  implements AuthenticationEntryPoint, Serializable {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        // 设置响应头
        response.setStatus(200);
        // 响应类型
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        // 设置返回的提示语句
        String msg = "请求访问："+request.getRequestURI()+"，认证失败，无法访问系统资源";

        com.qf.util.R r1 = R.fail(40003,msg);
        // 将Map转换为JSON字符串
        // 此处的JSON是阿里fastjson的jar包,需要导入
        String r = JSON.toJSONString(r1);
        // 响应出去
        response.getWriter().println(r);
    }
}
